// WEB浏览器视图，作为顶级的Web页面容器
import QtQuick 2.2
import QtQuick.Controls 1.1
import QtWebView 1.1
import QtWebEngine 1.2
import QtQuick.Layouts 1.1
import QtQuick.Controls.Styles 1.2

import "../widgets/webbrowser" as WebBrowserWidgets

import com.yihu.xchat.model 1.0

Rectangle {
    id: root

    Component.onCompleted: root.load(ConfigSingleton.cloudDesktopHome);

    // profiles
    property QtObject defaultProfile: WebEngineProfile {
        storageName: "Default"
    }

    property QtObject otrProfile: WebEngineProfile {
        offTheRecord: true
    }

    // web page container window and browser dialog
    property Component webPageContainer: WebBrowserWidgets.WebPageContainer {
        browserRoot: root
    }

    property Component browserDialogComponent: WebBrowserWidgets.BrowserDialog {
        onClosing: destroy()
    }

    function createWindow(profile) {
        var newWindow = webPageContainer.createObject(root)
        newWindow.currentWebView.profile = profile
        profile.downloadRequested.connect(newWindow.onDownloadRequested)
        return newWindow
    }

    function createDialog(profile) {
        var newDialog = browserDialogComponent.createObject(root)
        newDialog.currentWebView.profile = profile
        return newDialog
    }

    function load(url) {
        var browserWindow = createWindow(defaultProfile)
        browserWindow.currentWebView.url = url
    }
}
